package com.msds.parent.common.util;

import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

/**
 * Poi的工具类
 * @author zxj
 *
 */
@Component("poiUtil")
public class PoiUtil
{
	/**
	 * 导出Xls
	 * @param sheetName
	 * @param dataList 外面的List是行记录,里面的List是列
	 */
	public void exportXls(OutputStream os,String sheetName,List<List<String>> dataList)
	{
		try
		{
			/* 创建一个xls文件 */
			Workbook workbook = new HSSFWorkbook();
			/* 创建sheet */
			Sheet sheet = workbook.createSheet(sheetName);
			
			int count = 0 ; 
			for (Iterator iterator = dataList.iterator(); iterator.hasNext();)
			{
				List<String> dataTempList = (List<String>) iterator.next();
				/* 创建行 */
				Row row = sheet.createRow(count);
				
				int cellCount = 0 ; 
				for (Iterator iterator2 = dataTempList.iterator(); iterator2.hasNext();)
				{
					String data = (String) iterator2.next();
					/* 创建第一列 */
					Cell cell = row.createCell(cellCount);
					cell.setCellValue(data);
					cellCount ++ ; 
				}
				
				count ++ ; 
			}
			
			/* 将xls写到硬盘上 */
			workbook.write(os);
			os.close();
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("导出Xls失败了",e);
		}
	}
}
